<script setup>
import {User, Lock, Key} from "@element-plus/icons-vue";
    import {ElMessage} from "element-plus";
    import {ref} from "vue";
    import userApi from "@/api/user.js";
    import captchaApi from "@/api/captcha.js";

    const user = ref({
        name: '',
        password: '',
        captcha: ''
    })

    //表单正则表达式验证
    const rules = ref({
        name: [
            {required: true, message:'请输入用户名', trigger:"blur"}
        ],
        password: [
            {required: true, message: '请输入密码', trigger:"blur"}
        ],
        captcha: [
            {required: true, message: '请输入验证码', trigger:"blur"}
        ]
    })

    import {useRouter} from "vue-router";
    const router = useRouter();
    import {useTokenStore} from "@/store/token.js";
    const tokenStore = useTokenStore();

    const login = () => {
        userApi.login(user.value).then(result => {
            console.log('login')
            if (result.code == 0) {
                ElMessage.success(result.msg);
                //将得到的token存到pinia中
                tokenStore.setToken(result.data, remember.value);
                router.push('/');
            } else {
                ElMessage.error(result.msg);
                loadCaptcha()
            }
        })
    }

    //跳转忘记密码页面
    const userForgotPassword = () => {
        router.push('/userForgotPassword');
    }

    const captcha = ref('')
    const loadCaptcha = () => {
        captchaApi.captcha().then(result => {
            if (result.code == 0) {
                captcha.value = result.data
            }
        })
    }
    loadCaptcha()

    //记录是否勾选记住密码
    const remember = ref(false)
</script>

<template>
    <div class="login-bg">
        <!-- 登录表单 -->
        <el-form class="form-login" ref="form" size="large" autocomplete="off" :model="user" :rules="rules">
            <el-form-item>
                <h1>登录</h1>
            </el-form-item>
            <el-form-item prop="name">
                <el-input :prefix-icon="User" placeholder="请输入用户名" v-model="user.name"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input name="password" :prefix-icon="Lock" type="password" placeholder="请输入密码"
                          v-model="user.password"></el-input>
            </el-form-item>
            <el-form-item prop="captcha">
                <el-input name="captcha" :prefix-icon="Key" placeholder="请输入验证码" v-model="user.captcha"></el-input>
                <!--<img width="160px" src="http://locolhost:5173/api/captcha"/>-->
                <img width="160px" :src="captcha" @click="loadCaptcha"/>
            </el-form-item>
            <el-form-item class="flex">
                <div class="flex">
                    <el-checkbox v-model="remember">记住我</el-checkbox>
                    <el-link type="primary" :underline="false" @click="userForgotPassword">忘记密码？</el-link>
                </div>
            </el-form-item>
            <!-- 登录按钮 -->
            <el-form-item>
                <el-button class="button" type="primary" auto-insert-space @click="login">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<style scoped>
    .login-bg {
        width: 100%;
        height: 100vh;
        background-image: url('@/assets/bg.jpg');
        background-repeat: no-repeat;
        background-position: center;
        background-attachment: fixed;
        background-size: 100% 100%;
        position:absolute;
    }

    .form-login {
        width: 280px;
        padding: 20px;
        position: absolute;
        top: 20%;
        left: calc(50% - 150px);
        background-color: #FFF;
        box-shadow: 10px 10px 30px #000;
        opacity: 0.9;
    }
</style>